import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  getOrderListService,
  getOrderDetailService,
  deleteOrderService,
  addOrderService,
  submitOrderService,
  getPurchasedListService,
  changeStatusService,
  getBossOrderListService
} from '@/api/order'
export const useOrderStore = defineStore(
  'order',
  () => {
    const order = ref([])
    const bossOrderList = ref([])
    const getOrderList = async (id) => {
      const res = await getOrderListService(id)
      order.value = res.data
    }
    const getOrderListById = async (id) => {
      return await getOrderDetailService(id)
    }
    //根据id删除订单
    const deleteOrderById = async (id) => {
      return await deleteOrderService(id)
    }
    //添加订单

    const addOrder = async (data) => {
      return await addOrderService(data)
    }

    //提交订单
    const submitOrder = async (id) => {
      return await submitOrderService(id)
    }

    //获取已购订单
    const getOrderListByUserId = async (userId) => {
      const res = await getPurchasedListService(userId)
      order.value = res.data
    }

    //改变订单的状态为3即在用户列表不显示的状态

    const changeOrderStatus = async (id) => {
      await changeStatusService(id)
    }

    //获取商家的订单列表

    const getBossOrderList = async (id) => {
      const res = await getBossOrderListService(id)
      bossOrderList.value = res.data
      console.log(res.data)
      console.log(bossOrderList.value)
    }
    return {
      order,
      bossOrderList,
      getOrderList,
      getOrderListById,
      deleteOrderById,
      addOrder,
      submitOrder,
      getOrderListByUserId,
      changeOrderStatus,
      getBossOrderList
    }
  },
  {
    persist: true
  }
)
